import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
//import { MainData,MenuData} from './main.model';
import { MenuData } from './menu';
import { MenuService } from './menu.service';



@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
  providers: [ MenuService ]
})
export class AppComponent implements OnInit{
  title = 'Menu菜单测试';
  //获取菜单的数据
  menu = this.menu;
  
  /**
   * 构造方法
   */
  constructor(
    private router: Router, private menuService: MenuService) { }

  /**
   * 初始化
   */
  ngOnInit() {
    this.menuService.getJSON().subscribe(data => {
      this.menu = data;
      //console.log(data); 
      
    })
  }
  /**
   * 是否有子节点
   * @param one 
   */
  isLeaf(one: MenuData) {
    return !one.children || !one.children.length;
  }
  /**
   * 点击
   * @param one 
   */
  itemClicked(one: MenuData) {
    if (!this.isLeaf(one)) {
      one.isExpend = !one.isExpend;
    } else {
      this.router.navigate([one.url]);
    }
  }

}
